<?php

require_once WACT_ROOT . '/db/db.inc.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/site.php';

class CapFilter {
    function doFilter(&$vars){
    	if($vars['published']==1) {
    		if(isset($vars['status']))
    			$status='Published (shown)';
    		else $status='Published (waiting)';
    	} else if($vars['denied']==1)
    		$status='Denied';
    		else $status='Pending';
    	$vars['status']=$status;

    	$premium = $vars['premium'];

    	if(isset($premium) && $premium==1){
    		$vars['prem']='1';
    	}

        if ($premium) $vars['premium_show'] = 1;
    }
}

class Player{
	function Player(){
	}

    function insertPlayer(&$data) {
        $site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);

    	$premium = $data->get('premium');
    	if (!$premium) $data->set('premium',0);

    	$premiumVideo = $data->get('premiumVideo');
    	if (!$premiumVideo) $data->set('premiumVideo',0);

        $Record =& DBC::NewRecord($data);
        return $Record->insertId('sport_players',
            array('user_id', 'category_id','category2_id', 'date_added', 'class',
            'photo_id', 'photo_url', 'name', 'gender', 'weight', 'height', 'position',
            'school_name', 'school_address', 'school_city', 'school_state', 'school_zip',
            'coach_name', 'coach_phone',
            'gpa', 'year_graduation', 'team',
            'description', 'why_official_list',
            'projected_level', 'sat', 'contact_phone',
            'interest_offers', 'published','site_id', 'premium','premiumVideo'),
            'player_id');

    }

    function getPublicURLByID($id) {
        $url = 'players/details.htm?id='.$id;
    	if (APP_NAME!='.') $url = '/'.APP_NAME.'/'.$url;
    	else  $url = '/'.$url;
    	return $url;
    	//return '/'.APP_NAME.'/'.'pub.player.php?player_id='.$id;
    }

    function getIDByName($name) {
    	$site_id = Site::getSiteID();
     	$rec =& DBC::FindRecord("SELECT player_id
     	from sport_players P
     	where P.name = ".DBC::makeLiteral($name)."
        and P.site_id = ".DBC::makeLiteral($site_id)."");
     	if ($rec) return $rec->get('player_id');
     	else return 0;
    }

     function getPublicURLByName($name) {
     		$id = Player::getIDByName($name);
     		$url = Player::getPublicURLByID($id);
     		return $url;
     }

    function &getRecord($id) {
        return DBC::FindRecord("SELECT U.*, P.*, C1.name as category1, C2.name as category2
        FROM sport_players P
        left outer join sport_users U on P.user_id = U.user_id
        left outer join sport_categories C1 on P.category_id = C1.category_id
     	left outer join sport_categories C2 on P.category2_id = C2.category_id
        WHERE P.player_id = ". DBC::makeLiteral($id));
    }

    function updatePlayer($id, &$data) {
    	$site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);

    	$premium = $data->get('premium');
    	if (!$premium) $data->set('premium',0);

    	$premiumVideo = $data->get('premiumVideo');
    	if (!$premiumVideo) $data->set('premiumVideo',0);

        $Record =& DBC::NewRecord($data);
        $Record->update('sport_players',
            array('category_id','category2_id', 'class',
            'photo_id', 'photo_url', 'name', 'gender', 'weight', 'height', 'position',
            'school_name', 'school_address', 'school_city', 'school_state', 'school_zip',
            'coach_name', 'coach_phone',
            'gpa', 'year_graduation', 'team',
            'description', 'why_official_list',
            'projected_level', 'sat', 'contact_phone',
            'interest_offers', 'published','site_id','premium','premiumVideo'),
            "player_id=". DBC::makeLiteral($id));
    }

    function &getUserPlayesList(&$Pager,$user_id,$option=null) {
    	$site_id = Site::getSiteID();
    	$isGlobal = Site::IsGlobal();
    	$query="SELECT P.*,C.name as \"category1\",A.name as \"category2\", if(P.date_added<=now(),'true',NULL) as \"status\" ";
    	$query.="  FROM ";
    	$query.=" (sport_players P left outer join sport_categories C on P.category_id= C.category_id) ";
    	$query.=" left outer join sport_categories A on category2_id=A.category_id ";
    	$query.=" where user_id=".DBC::makeLiteral($user_id);
    	if($option!=null) $query.=" and P.class=".DBC::makeLiteral($option);
        $query.=" and (P.site_id = ".DBC::makeLiteral($site_id);
        if ($isGlobal) $query.=" or 1=1) ";
    	else $query.=") ";
    	//error_log("[SQL:] $query");
       	return DBC::NewPagedRecordSet($query,$Pager,new CapFilter());
    }

    function getNewPlayersCount() {
        $site_id = Site::getSiteID();
    	return DBC::getOneValue("Select count(*) from sport_players
    	where (published = 0 or published is null) and (denied=0 or denied is null)
        and site_id = ".DBC::makeLiteral($site_id));
    }

    function denyPlayer($player_id) {
        $data = new DataSpace();
        $data->set('denied',1);
        $data->set('published',0);
    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_players',
            array('denied', 'published'),
            "player_id=". DBC::makeLiteral($player_id));
    }

    function publishPlayer($player_id) {
        $data = new DataSpace();
        $data->set('denied',0);
        $data->set('published',1);
    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_players',
            array('denied', 'published'),
            "player_id=". DBC::makeLiteral($player_id));
    }

    // get the next new article
    function &getNextNewPlayer($current_player_id) {
    	$site_id = Site::getSiteID();
        $sql = "select A.* from sport_players A, sport_players A2
        where A.player_id <> A2.player_id
        and A2.player_id = ".DBC::makeLiteral($current_player_id)."
        and A.date_added <= A2.date_added
        and (A.published = 0  or  A.published is null)
        and A.site_id = ".DBC::makeLiteral($site_id)."
        and A2.site_id = ".DBC::makeLiteral($site_id)."
        order by A.date_added desc limit 1";
        return DBC::FindRecord($sql);
    }

    function &getPlayersList(&$Pager, &$request, &$responseModel,$public=false) {
        $site_id = Site::getSiteID();
        $isGlobal = Site::IsGlobal();
        //restore filter from session
     	Session::loadResponseModel($responseModel,'filter.players');

     	$sql = "select P.*, U.*, C1.name as category1, C2.name as category2
     	from sport_players P
     	left outer join sport_users U on P.user_id = U.user_id
     	left outer join sport_categories C1 on P.category_id = C1.category_id
     	left outer join sport_categories C2 on P.category2_id = C2.category_id
     	where 1=1 and P.published=1 ".Player::getPlayersFilter($request, $responseModel);
     	//if($public) $sql.=" and P.date_added<=now() ";
        $sql.=" and (P.site_id = ".DBC::makeLiteral($site_id);
        if ($isGlobal) $sql.=" or 1=1) ";
    	else $sql.=") ";
     	$sql.=" order by ".Player::getPlayersOrder($request, $responseModel);
     	//print $sql;
        return DBC::NewPagedRecordSet($sql, $Pager,new CapFilter());
     }

     function &getNewPlayersList(&$Pager, &$request, &$responseModel) {
     	$site_id = Site::getSiteID();
        $sql = "select P.*, U.*, C1.name as category1, C2.name as category2
     	from sport_players P
     	left outer join sport_users U on P.user_id = U.user_id
     	left outer join sport_categories C1 on P.category_id = C1.category_id
     	left outer join sport_categories C2 on P.category2_id = C2.category_id
        where 1=1 and (P.published=0 or P.published is null)
        and P.site_id = ".DBC::makeLiteral($site_id)."
     	order by P.date_added desc ";

     	return DBC::NewPagedRecordSet($sql, $Pager);

      }

     function getPlayersFilter(&$request, &$responseModel) {
      	$where = '';
      	$filter_player_id = $responseModel->get('filter_player_id');
        $player_id = $responseModel->get('player_id');
      	if ($filter_player_id)  $where .= ' and P.player_id = '.DBC::makeLiteral($player_id);

        $filter_category1 = $responseModel->get('filter_category1');
        $category1 = $responseModel->get('category1');
        if ($filter_category1)  $where .= ' and P.category_id = '.DBC::makeLiteral($category1);

        $filter_category2 = $responseModel->get('filter_category2');
        $category2 = $responseModel->get('category2');
        if ($filter_category2)  $where .= ' and P.category2_id = '.DBC::makeLiteral($category2);

        $filter_class = $responseModel->get('filter_class');
        $class_value = $responseModel->get('class');
        if ($filter_class)  $where .= ' and P.class = '.DBC::makeLiteral($class_value);

        return $where;
      }

      function getPlayersOrder(&$request, &$responseModel) {
      	$sortby = 'P.date_added desc';
      	$sortby = $responseModel->get('sort');
      	if ($sortby) return 'P.'.$sortby;
      	else return 'P.date_added desc';
      }


    function &getClassesList($none=false){
    	$period1=0;
    	$period2=6;
    	$date=getdate();
    	$classes=array();
    	if($none) $classes[0]='(ALL)';
    	$curyear=$date['year'];
    	$array=array($curyear=>$curyear);
    	for($i=$period1;$i>=1;$i--)
    		$classes[$curyear-$i]=$curyear-$i;
    	$classes[$curyear]=$curyear;
    	for($i=1;$i<$period2;$i++)
    		$classes[$curyear+$i]=$curyear+$i;
    	return $classes;
    }

    function &getGendersList(){
    	$genders = array("0"=>"Male", "1"=>"Female");
        return $genders;
    }

    function &getGenderName($id) {
    	$glist =& Player::getGendersList();
    	return $glist[$id];
    }


    function &getTeamsList(){
    	$teams = array("0"=>"(ALL)", "Freshman"=>"Freshman", "JV"=>"JV", "Varsity"=>"Varsity" );
        return $teams;

    }

    function &getCollegeLevels(){
    	$levels = array("(ALL)"=>"(ALL)", "JUCO"=>"JUCO", "NAIA"=>"NAIA", "Division I"=>"Division I",
        "Division II"=>"Division II", "Low DI"=>"Low DI", "Mid DI"=>"Mid DI");
        return $levels;

    }

    function getPlayersCount($user_id) {
    	$site_id = Site::getSiteID();
    	$isGlobal = Site::IsGlobal();
    	$sql =  "Select count(*) from sport_players
    	where user_id=".DBC::makeLiteral($user_id)."
    	and (site_id = ".DBC::makeLiteral($site_id);
    	if ($isGlobal) $sql.=" or 1=1) ";
    	else $sql.=") ";
    	return DBC::getOneValue($sql);
    }


    function delete($id) {
    	$query="DELETE FROM sport_players WHERE player_id=". DBC::makeLiteral($id);
        DBC::execute($query);
    }


}

?>
